/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: MissionPanel.java 604 2010-07-26 19:19:28Z Harry $
 *   $LastChangedDate: 2010-07-26 19:19:28 +0000 (Mon, 26 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.component;

import battlefield.entity.Mission;
import battlefield.entity.Player;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author xiankun
 */
public class MissionPanel extends JPanel {

    private HashMap<String, Mission> allMission = null;
    private Timer timer;
    private MissionButton selectedButton = null;
    private MissionButton[] buttons;
    private ActionListener obj;
    private String actionCommand;

    public MissionPanel(Player player) {
        this.allMission = player.getMissions();
        initComponent();
    }

    private void initComponent() {
        this.setOpaque(false);
        FlowLayout missionBodyGrid = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(missionBodyGrid);
        populateMissions();
        this.setSize(1000, 150);
        this.setVisible(true);
    }

    private void populateMissions() {


        buttons = new MissionButton[allMission.size()];

        Object[] tempObj = allMission.values().toArray();

        Mission[] tempList = new Mission[allMission.size()];

        for (Object o : tempObj) {
            Mission tempMission = (Mission) o;
            tempList[tempMission.getMissionLevel()] = tempMission;
        }
        int i = 0;
        for (Mission mission : tempList) {
            MissionButton missionButton = missionItem(mission);
            this.add(missionButton);
            buttons[i] = missionButton;
            i++;
        }
    }

    public void updateMissions(Player player) {

        this.allMission = player.getMissions();
        this.removeAll();
        this.setLayout(new FlowLayout());
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = null;
        }

        Object[] tempObj = allMission.values().toArray();

        Mission[] tempList = new Mission[allMission.size()];

        for (Object o : tempObj) {
            Mission tempMission = (Mission) o;
            tempList[tempMission.getMissionLevel()] = tempMission;
        }
        int i = 0;
        for (Mission mission : tempList) {
            MissionButton newButton = missionItem(mission);
            newButton.addActionListener(obj);
            newButton.setActionCommand(actionCommand);
            this.add(newButton);
            buttons[i] = newButton;
            i++;
        }

        this.validate();
    }

    public MissionButton missionItem(Mission mission) {
        GridLayout missionItemLayout = new GridLayout(4, 0);
        MissionButton missionItemButton = new MissionButton(mission);
        missionItemButton.setLayout(missionItemLayout);
        missionItemButton.setPreferredSize(new Dimension(150, 150));

        missionItemButton.setName(mission.getMissionName());
        missionItemButton.setEnabled(mission.isEnable());
        return missionItemButton;
    }

    public void addActionListener(ActionListener obj, String actionCommand) {
        this.obj = obj;
        this.actionCommand = actionCommand;
        for (MissionButton missionButton : buttons) {
            System.out.println("add action listener");
            missionButton.addActionListener(obj);
            missionButton.setActionCommand(actionCommand);
        }
    }

    @Override
    protected void finalize() throws Throwable {
        if (timer != null && timer.isRunning()) {
            timer.stop();
        }
        timer = null;
        super.finalize();
    }
}
